import collections


class Solution(object):
    def calculate(self, s):
        """
        :type s: str
        :rtype: int
        """
        stacks = collections.deque()
        stacks.append(collections.deque())
        s += '+'
        last_op = ['+']
        value = 0
        left_index = 0
        right_index = 0
        n = len(s)
        while right_index < n:
            if s[right_index] == '(':
                stacks.append(collections.deque())
                last_op.append('+')
                left_index = right_index + 1
            else:
                if s[right_index] in {'+', '-', '*', '/', ')'}:
                    if right_index - left_index > 0:
                        num = int(s[left_index: right_index])
                        if last_op[-1] == '+':
                            stacks[-1].append(num)
                        if last_op[-1] == '-':
                            stacks[-1].append(-num)
                        if last_op[-1] == '*':
                            last_num = stacks[-1].pop()
                            stacks[-1].append(last_num * num)
                        if last_op[-1] == '/':
                            last_num = stacks[-1].pop()
                            if last_num * num < 0:
                                stacks[-1].append(-(abs(last_num) // abs(num)))
                            else:
                                stacks[-1].append(last_num // num)
                    if s[right_index] == ')':
                        num = sum(stacks[-1])
                        stacks.pop()
                        last_op.pop()
                        if last_op[-1] == '+':
                            stacks[-1].append(num)
                        if last_op[-1] == '-':
                            stacks[-1].append(-num)
                        if last_op[-1] == '*':
                            last_num = stacks[-1].pop()
                            stacks[-1].append(last_num * num)
                        if last_op[-1] == '/':
                            last_num = stacks[-1].pop()
                            if last_num * num < 0:
                                stacks[-1].append(- (abs(last_num) // abs(num)))
                            else:
                                stacks[-1].append(last_num // num)
                    else:
                        last_op[-1] = s[right_index]
                    left_index = right_index + 1
            right_index += 1

        return sum(stacks[0])


data = Solution()
s = "-5-3*2"
print(data.calculate(s))
s = "2*(5+5*2)/3+(6/2+8)"
print(data.calculate(s))
s = "(6-4/2)"
print(data.calculate(s))
s = "6-4/2*3"
print(data.calculate(s))
s = "(2+6*3+5-(3*14/7+2)*5)+3"
print(data.calculate(s))
s = "5-3/2"
print(data.calculate(s))